﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace LightningForms
{
    public class BaseTextControl : TextBox
    {
        public bool Required
        {
            get
            {
                object value = this.ViewState["Required"];
                if (value != null) return (bool)value;
                return false;
            }
            set { this.ViewState["Required"] = value; }
        }
        public virtual string MinValue
        {
            get
            {
                object value = this.ViewState["MinValue"];
                if (value != null) return value.ToString();
                return string.Empty;
            }
            set { this.ViewState["MinValue"] = value; }
        }
        public virtual string MaxValue
        {
            get
            {
                object value = this.ViewState["MaxValue"];
                if (value != null) return value.ToString();
                return string.Empty;
            }
            set { this.ViewState["MaxValue"] = value; }
        }
        public string ControlToCompare
        {
            get
            {
                object value = this.ViewState["ControlToCompare"];
                if (value != null) return value.ToString();
                return string.Empty;
            }
            set { this.ViewState["ControlToCompare"] = value; }
        }
        public string ValueToCompare
        {
            get
            {
                object value = this.ViewState["ValueToCompare"];
                if (value != null) return value.ToString();
                return string.Empty;
            }
            set { this.ViewState["ValueToCompare"] = value; }
        }
        public ValidationCompareOperator CompareOperator
        {
            get
            {
                object value = this.ViewState["CompareOperator"];
                if (value != null) return (ValidationCompareOperator)value;
                return ValidationCompareOperator.Equal;
            }
            set { this.ViewState["CompareOperator"] = value; }
        }
        public string InvalidTypeErrorMessage
        {
            get
            {
                object value = this.ViewState["InvalidTypeErrorMessage"];
                if (value != null) return value.ToString();
                return Properties.Resources.MESSAGE_TYPEVALIDATOR;
            }
            set { this.ViewState["InvalidTypeErrorMessage"] = value; }
        }
        public string RequiredErrorMessage
        {
            get
            {
                object value = this.ViewState["RequiredErrorMessage"];
                if (value != null) return value.ToString();
                return Properties.Resources.MESSAGE_REQUIRED;
            }
            set { this.ViewState["RequiredErrorMessage"] = value; }
        }
        public string RangeErrorMessage
        {
            get
            {
                object value = this.ViewState["RangeErrorMessage"];
                if (value != null) return value.ToString();
                return Properties.Resources.MESSAGE_RANGE;
            }
            set { this.ViewState["RangeErrorMessage"] = value; }
        }
        public string CompareErrorMessage
        {
            get
            {
                object value = this.ViewState["CompareErrorMessage"];
                if (value != null) return value.ToString();
                return Properties.Resources.MESSAGE_COMPARE;
            }
            set { this.ViewState["CompareErrorMessage"] = value; }
        }
        protected ValidationDataType DataType {
            get
            {
                object value = this.ViewState["DataType"];
                if (value != null) return (ValidationDataType)value;
                return ValidationDataType.String;
            }
            set { this.ViewState["DataType"] = value; }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            Helper.AddSiblingControl(this, Validators.TypeCompare(this, this.DataType, this.InvalidTypeErrorMessage));
            //SE O CAMPO É REQUERIDO
            if (this.Required)
                Helper.AddSiblingControl(this, Validators.Required(this, this.RequiredErrorMessage));
            //SE HÁ UM INTERVALO ESTIPULADO
            if (!string.IsNullOrEmpty(this.MinValue) || !string.IsNullOrEmpty(this.MaxValue))
                Helper.AddSiblingControl(
                    this,
                    Validators.Range(
                        this,
                        ValidationDataType.Date,
                        this.MaxValue != null ? this.MaxValue : string.Empty,
                        this.MinValue != null ? this.MinValue : string.Empty,
                        this.RangeErrorMessage));
            //SE HÁ UM VALOR OU CAMPO A SER COMPARADO
            if (!string.IsNullOrEmpty(this.ControlToCompare))
                Helper.AddSiblingControl(
                    this,
                    Validators.ControlCompare(
                        this,
                        this.DataType,
                        this.CompareOperator,
                        this.ControlToCompare,
                        this.CompareErrorMessage));
            if (!string.IsNullOrEmpty(this.ValueToCompare))
                Helper.AddSiblingControl(
                    this,
                    Validators.ValueCompare(
                        this,
                        this.DataType,
                        this.CompareOperator,
                        this.ValueToCompare,
                        this.CompareErrorMessage));
        }

        public BaseTextControl()
        {
            this.Required = true;
            this.CssClass = "ui-widget-content";
        }

        protected override void OnInit(EventArgs e)
        {
            this.Page.Init += Page_Init;
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            Helper.RegisterScripts(this);
            Helper.RegisterStyles(this);
            base.OnPreRender(e);
        }
    }
}